package com.mygame.message.rpc;

import com.mygame.message.channel.AbstractGameChannelHandlerContext;
import com.mygame.common.protobuf.ExternalMessage;

public class RPCEventContext<T>  {

    private ExternalMessage request;
    private T data;//这个用于存储缓存的数据，因为不同的服务的数据结构是不同的，所以这里使用泛型
    private AbstractGameChannelHandlerContext ctx;
    public RPCEventContext(T data,ExternalMessage request, AbstractGameChannelHandlerContext ctx) {
        super();
        this.request = request;
        this.ctx = ctx;
        this.data = data;
    }

    public T getData() {
        return data;
    }
    public void sendRpcResponse(ExternalMessage response) {
//        GameMessageHeader header = response.getHeader();
//        MessageType mesasageType = header.getMesasageType();
//        if(mesasageType != MessageType.RPC_RESPONSE) {
//            throw new IllegalArgumentException(response.getClass().getName() + " 参数类型不对，不是RPC的响应数据对象");
//        }
//        GameMessageHeader requestHeander = request.getHeader();
//        header.setToServerId(requestHeander.getFromServerId());
//        header.setFromServerId(requestHeander.getToServerId());
//        header.setClientSeqId(requestHeander.getClientSeqId());
//        header.setClientSendTime(requestHeander.getClientSendTime());
//        header.setPlayerId(requestHeander.getPlayerId());
//        header.setServerSendTime(System.currentTimeMillis());
        response.setFromServerId(request.getToServerId());
        response.setToServerId(request.getFromServerId());
        response.setClientSeqId(request.getClientSeqId());
        ctx.writeRPCMessage(response, null);
    }



}
